package com.mzba.easy.weibo.util;

import java.util.Set;

import android.graphics.drawable.Drawable;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.widget.EditText;

/**
 * 
 * @author 06peng
 *
 */
public class EditTextEmotionUtil {
	
	private static ImageGetter imageGetter = new Html.ImageGetter() {
        @Override
        public Drawable getDrawable(String source) {
            Drawable drawable = null;
			String sourceName = AppContext.getContext().getPackageName() + ":drawable/" + source;
			int id = AppContext.getContext().getResources().getIdentifier(sourceName, null, null);
			if (id != 0) {
				drawable = AppContext.getContext().getResources().getDrawable(id);
				if (drawable != null) {
					drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
				}
			}
			return drawable;
        }
	};

	public static void addText(EditText edit, String content) {
		edit.append(content);
		
		String str = edit.getText().toString();
		if (AppContext.getInstance().getFaceMap() != null) {
			// 对表情符以img标记进行修饰，改用drawable显示出来
			Set<String> keys = AppContext.getInstance().getFaceMap().keySet();
			for (String key : keys) {
				if (str.contains(key)) {
					str = str.replace(key, "<img src='" + AppContext.getInstance().getFaceMap().get(key) + "'>");
				}
			}
		}
		edit.setText(Html.fromHtml(str, imageGetter, null));
	}
}
